package com.ajguan.library;


import ohos.agp.utils.TextTool;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * 日志类
 */
public final class ELog {
    /**
     * Log TAG
     */
    private static final String TAG_LOG = "[] ";
    /**
     * Log DOMAIN ID
     */
    private static final int DOMAIN_ID = 0x00201;
    /**
     * Log label
     */
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, DOMAIN_ID, TAG_LOG);

    /**
     * Log format
     */
    private static final String LOG_FORMAT = "%s: %s";

    /**
     * Priority constant for the println method; use Log.v.
     */
    public static final int VERBOSE = 2;

    /**
     * Priority constant for the println method; use Log.d.
     */
    public static final int DEBUG = 3;

    /**
     * Priority constant for the println method; use Log.i.
     */
    public static final int INFO = 4;

    /**
     * Priority constant for the println method; use Log.w.
     */
    public static final int WARN = 5;

    /**
     * Priority constant for the println method; use Log.e.
     */
    public static final int ERROR = 6;

    /**
     * Priority constant for the println method.
     */
    public static final int ASSERT = 7;

    /**
     * log是否可用
     */
    private static boolean isEnabled = true;


    private ELog() {
    }

    /**
     * 设置log是否可用
     *
     * @param enableBoolean 是否可用
     */
    public static void setEnable(boolean enableBoolean) {
        isEnabled = enableBoolean;
    }

    /**
     * 检查log是否可用
     *
     * @return boolean isEnabled
     */
    public static boolean isEnable() {
        return isEnabled;
    }

    private static void log(LEVEL level, String tag, String msg, Throwable thr) {
        if (!isEnabled) {
            return;
        }

        log2console(level, tag, msg, thr);
    }

    private static void log2console(LEVEL level, String tag, String msg, Throwable thr) {
        boolean isFilter = false;
        if (!isFilter) {
            switch (level) {
                case DEBUG:
                    if (thr == null) {
                        HiLog.debug(LABEL_LOG, LOG_FORMAT, tag, msg);
                    } else {
                        HiLog.debug(LABEL_LOG, LOG_FORMAT, tag, msg, thr);
                    }
                    break;
                case INFO:
                    if (thr == null) {
                        HiLog.info(LABEL_LOG, LOG_FORMAT, tag, msg);
                    } else {
                        HiLog.info(LABEL_LOG, LOG_FORMAT, tag, msg, thr);
                    }
                    break;
                case WARN:
                    if (thr == null) {
                        HiLog.warn(LABEL_LOG, LOG_FORMAT, tag, msg);
                    } else if (TextTool.isNullOrEmpty(msg)) {
                        HiLog.warn(LABEL_LOG, LOG_FORMAT, tag, thr);
                    } else {
                        HiLog.warn(LABEL_LOG, LOG_FORMAT, tag, msg, thr);
                    }
                    break;
                case ERROR:
                    if (thr == null) {
                        HiLog.error(LABEL_LOG, LOG_FORMAT, tag, msg);
                    } else {
                        HiLog.error(LABEL_LOG, LOG_FORMAT, tag, msg, thr);
                    }
                    break;
                default:
                    break;
            }
        }
    }

    public static void d(String tag, String msg) {
        log(LEVEL.DEBUG, tag, msg, null);
    }

    public static void d(String tag, String msg, Throwable thr) {
        log(LEVEL.DEBUG, tag, msg, thr);
    }

    public static void i(String tag, String msg) {
        log(LEVEL.INFO, tag, msg, null);
    }

    public static void i(String tag, String msg, Throwable thr) {
        log(LEVEL.INFO, tag, msg, thr);
    }

    public static void w(String tag, String msg) {
        log(LEVEL.WARN, tag, msg, null);
    }

    public static void w(String tag, String msg, Throwable thr) {
        log(LEVEL.WARN, tag, msg, thr);
    }

    public static void w(String tag, Throwable thr) {
        log(LEVEL.WARN, tag, "", thr);
    }

    public static void e(String tag, String msg) {
        log(LEVEL.ERROR, tag, msg, null);
    }

    public static void e(String tag, String msg, Throwable thr) {
        log(LEVEL.ERROR, tag, msg, thr);
    }

    /**
     * 等级枚举
     *
     * @author jjzheng
     */
    public enum LEVEL {
        DEBUG(3, "D"),
        INFO(4, "I"),
        WARN(5, "W"),
        ERROR(6, "E");

        final String levelString;
        final int level;

        private LEVEL(int level, String levelString) {
            this.level = level;
            this.levelString = levelString;
        }

        /**
         * 获取日志级别字符串
         *
         * @return String
         */
        public String getLevelString() {
            return this.levelString;
        }

        /**
         * 获取日志级别
         *
         * @return int
         */
        public int getLevel() {
            return this.level;
        }
    }


}
